package com.example.administrator.ygddriver.into.login.driverjoin.small.smallmessage.audit;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.search.route.DrivingRouteLine;
import com.example.administrator.ygddriver.MainActivity;
import com.example.administrator.ygddriver.R;
import com.example.administrator.ygddriver.base.BaseActivity;
import com.example.administrator.ygddriver.base.JGRequestBean;
import com.example.administrator.ygddriver.base.LogInformationBean;
import com.example.administrator.ygddriver.base.UserBean;
import com.example.administrator.ygddriver.into.login.driverjoin.small.smallmessage.SmallMessageActivity;
import com.example.administrator.ygddriver.into.login.driverjoin.weight.weightmessage.WeightMessageActivity;
import com.example.administrator.ygddriver.liteorm.LiteOrmTools;
import com.example.administrator.ygddriver.tool.SimpleToolbar;
import com.example.administrator.ygddriver.tool.UrlValuse;

/**
 * 审核界面
 */
public class AuditActivity extends BaseActivity implements View.OnClickListener {
    private SimpleToolbar simpleToolbar;
    private TextView mNextTv, mStatusTv, mOpinionTv;
    private String status, opinion;
    private ImageView imageView;
    private String c_drivertype;
    private AuditActivityReceiver auditActivityReceiver;


    @Override
    public int initLayout() {
        return R.layout.activity_audit;
    }

    @Override
    public void initView() {
        auditActivityReceiver = new AuditActivityReceiver();
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(UrlValuse.JG);
        registerReceiver(auditActivityReceiver, myIntentFilter);

        simpleToolbar = findViewById(R.id.activity_audit_stb);
        mNextTv = findViewById(R.id.activity_audit_next_tv);
        mStatusTv = findViewById(R.id.activity_audit_status_tv);
        mOpinionTv = findViewById(R.id.activity_audit_opinion_tv);
        imageView = findViewById(R.id.activity_audit_iv);
        mNextTv.setOnClickListener(this);
    }

    @Override
    public void initDate() {
        LiteOrmTools.getInformation(LogInformationBean.class, this, new LiteOrmTools.On<LogInformationBean>() {
            @Override
            public void hasData(LogInformationBean data) {
                c_drivertype = data.getC_drivertype();
                status = data.getC_status();
            }
        }, new LiteOrmTools.OFF() {
            @Override
            public void noData() {

            }
        });
        status = getIntent().getStringExtra("status");
        opinion = getIntent().getStringExtra("opinion");
        c_drivertype = getIntent().getStringExtra("c_drivertype");
        LiteOrmTools.getInformation(LogInformationBean.class, this, new LiteOrmTools.On<LogInformationBean>() {
            @Override
            public void hasData(LogInformationBean data) {
                c_drivertype = data.getC_drivertype();
            }
        }, new LiteOrmTools.OFF() {
            @Override
            public void noData() {

            }
        });
        if (status.equals("1")) {
            mStatusTv.setText("审核中");
            mOpinionTv.setText("我们将在3小时内审核完毕，请您耐心等待！");
            imageView.setImageResource(R.mipmap.ok);
            Resources resources = getBaseContext().getResources();
            Drawable drawable = resources.getDrawable(R.drawable.gray_rectangular);
            mNextTv.setBackground(drawable);
            mNextTv.setText("请您耐心等待");
        } else if (status.equals("3")) {
            mStatusTv.setText("审核未通过");
            mOpinionTv.setText(opinion);
            imageView.setImageResource(R.mipmap.no);
            Resources resources = getBaseContext().getResources();
            Drawable drawable = resources.getDrawable(R.drawable.red_rectangular);
            mNextTv.setBackground(drawable);
            mNextTv.setText("重新审核");
        } else {
            mStatusTv.setText("审核通过");
            mOpinionTv.setText("审核已通过，请使用");
            Resources resources = getBaseContext().getResources();
            Drawable drawable = resources.getDrawable(R.drawable.green_rectangular);
            mNextTv.setBackground(drawable);
            mNextTv.setText("开始使用");
        }
        setSimpleToolbar();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_audit_next_tv:
                if (status.equals("3")) {
                    if (c_drivertype.equals("1")) {
                        Intent intent = new Intent(this, SmallMessageActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        Intent intent = new Intent(this, WeightMessageActivity.class);
                        startActivity(intent);
                        finish();
                    }

                } else if (status.equals("1")) {

                }
                if (status.equals("2")) {
                    Intent intent = new Intent(this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }

                break;
        }
    }

    private void setSimpleToolbar() {
        simpleToolbar.setMainTitle("审核验证");
        simpleToolbar.setMainTitleColor("#000000");
        simpleToolbar.setToolColor("#ffffff");

    }

    public class AuditActivityReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            JGRequestBean jgRequestBean = bundle.getParcelable("jgRequestBean");

            mOpinionTv.setText(jgRequestBean.getC_opinion());
            status = jgRequestBean.getC_status();
            c_drivertype = jgRequestBean.getC_drivertype();
            if (jgRequestBean.getC_status().equals("2")) {
                mStatusTv.setText("审核通过");
                mOpinionTv.setText("审核已通过，请使用");
                Resources resources = getBaseContext().getResources();
                Drawable drawable = resources.getDrawable(R.drawable.green_rectangular);
                mNextTv.setBackground(drawable);
                mNextTv.setText("开始使用");
                status = "2";
            } else {
                mStatusTv.setText("审核未通过");
                imageView.setImageResource(R.mipmap.no);
                status = "3";
                Resources resources = getBaseContext().getResources();
                Drawable drawable = resources.getDrawable(R.drawable.red_rectangular);
                mNextTv.setBackground(drawable);
                mNextTv.setText("重新审核");
            }

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (auditActivityReceiver != null) {

        }
    }
}
